﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using ProjectEuler.Linq;

namespace ProjectEuler.Problems
{
    [EulerProblem(5, 232792560)]
    [Description("What is the smallest number divisible by each of the numbers 1 to 20?")]
    internal sealed class Problem005 : EulerProblem
    {
        public override Object Solve()
        {
            return Candidates().First(IsDivisibleByUpToTwenty);
        }

        private static IEnumerable<Int32> Candidates()
        {
            for (var i = Factor; i < Int32.MaxValue; i += Factor)
            {
                yield return i;
            }
        }

        private static readonly Func<Int32, Boolean> IsDivisibleByUpToTwenty =
            Numeric.IsMultipleOfAll(12, 14, 16, 18);

        private const Int32 Factor = 11 * 13 * 17 * 19 * 20;
    }
}
